using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.ContactsDomain;
using ShivamEMR.Model.DataInterfaces.ContactsDataInterf;
using ShivamEMR.Model;
//using ShivamEMR.Web.Areas.Contacts.Models;

namespace ShivamEMR.Web.Areas.Contacts.Controllers
{   
    public class TerritoryController : Controller
    {
		private readonly ICountryRepository countryRepository;
		private readonly ITerritoryRepository territoryRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public TerritoryController()
        {
            countryRepository = Registry.RepositoryFactory.GetCountryRepository();
            territoryRepository = Registry.RepositoryFactory.GetTerritoryRepository();
        }

        public TerritoryController(ICountryRepository countryRepository, ITerritoryRepository territoryRepository)
        {
			this.countryRepository = countryRepository;
			this.territoryRepository = territoryRepository;
        }

        //
        // GET: /Territory/

        public ViewResult Index()
        {
           // return View(territoryRepository.AllIncluding(territory => territory.Country, territory => territory.Cities));
            return View(territoryRepository.All);
        }

        //
        // GET: /Territory/Details/5

        public ViewResult Details(System.Guid id)
        {
            return View(territoryRepository.Find(id));
        }

        //
        // GET: /Territory/Create

        public ActionResult Create()
        {
			ViewBag.PossibleCountries = countryRepository.All;
            return View();
        } 

        //
        // POST: /Territory/Create

        [HttpPost]
        public ActionResult Create(Territory territory)
        {
            if (ModelState.IsValid) {
                territoryRepository.InsertOrUpdate(territory);
                territoryRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleCountries = countryRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Territory/Edit/5
 
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleCountries = countryRepository.All;
             return View(territoryRepository.Find(id));
        }

        //
        // POST: /Territory/Edit/5

        [HttpPost]
        public ActionResult Edit(Territory territory)
        {
            if (ModelState.IsValid) {
                territoryRepository.InsertOrUpdate(territory);
                territoryRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleCountries = countryRepository.All;
				return View();
			}
        }

        //
        // GET: /Territory/Delete/5
 
        public ActionResult Delete(System.Guid id)
        {
            return View(territoryRepository.Find(id));
        }

        //
        // POST: /Territory/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            var t = territoryRepository.Find(id);
            territoryRepository.Delete(t);
            territoryRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

